﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;


namespace MySchool
{
    public class DBHelperr
    {
        //数据库链接字符串
        private string str = @"Data Source=127.0.0.1;Initial Catalog=MyKTV;User ID=sa;Password=123456@qwe";

        //数据库链接Connection对象
        private SqlConnection connection;

        public SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection(str);
                }
                return connection;
            }
        }


        public void OpenConnection()
        {
            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }
            else if (Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
                Connection.Open();

            }
        }


        public void CloseConnection()
        {
            if (Connection.State == ConnectionState.Open
                || Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
            }

        }


    }
}

